package org.aplikator.server.descriptor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.aplikator.client.descriptor.FunctionDTO;
import org.aplikator.client.descriptor.WidgetDTO;
import org.aplikator.server.Context;
import org.aplikator.server.function.Executable;

public class Function extends ServerDescriptorBase implements Widget {

    private final Executable executable;

    private List<Property<? extends Serializable>> properties = new ArrayList<Property<? extends Serializable>>();

    public Function(String id, String readableName, Executable executable) {
        super(id);
        setLocalizationKey(readableName);
        this.executable = executable;
    }

    public Executable getExecutable() {
        return executable;
    }

    public List<Property<? extends Serializable>> getProperties() {
        return properties;
    }

    public Function addProperty(Property<? extends Serializable> property) {
        properties.add(property);
        return this;
    }

    public FunctionDTO getFunctionDTO(Context ctx) {
        FunctionDTO functionDTO = new FunctionDTO(this.getId(), this.getLocalizedName(ctx));
        functionDTO.setLocalizedName(this.getLocalizedName(ctx));
        for (Property<? extends Serializable> p : this.getProperties()) {
            functionDTO.addProperty(p.clientClone(ctx));
        }
        return functionDTO;
    }

    public WidgetDTO getWidgetDescriptor(Context ctx) {

        return getFunctionDTO(ctx);
    }

    public void registerProperties(Form form) {

    }

}
